<?php
    class product_edit_Controller extends controllerBase
    {
        public function process()
        {
            $this->aParams['product'] = $this->iId;
            $uri = URL::getLink('product/id/'.$this->iId);
            $oDatabase = new database();

            if(isset($_POST['tensp']))
            {
                // Edit upload location here
                $destination_path = getcwd().DIRECTORY_SEPARATOR;
                $iImageCount = $_FILES['avatar']['name'];
                $aImages = array();
                for($i = 0; $i < count($_FILES['avatar']['name']) ; $i++)
                {
                    $file_name=$_FILES['avatar']['name'][$i];
                    if($file_name != '' && !empty($file_name))
                    {                     
                        $target_path = $destination_path . basename($file_name);

                        if(@move_uploaded_file($_FILES['avatar']['tmp_name'][$i], "upload/".$file_name)) {
                            $aImages[] = $file_name;
                        }
                        else
                        {
                            $this->aParams['error'] = 'Không thể upload file!';
                        }
                    }
                }

                $this->aParams['result'] = DBObject::updataProduct($uri,$_POST['tensp'],$_POST['dongia'],$_POST['description'],$aImages,$_POST['category_uri'],$_POST['list_categories'],$_POST['tb_nhasanxuat'],$_POST['location_id']);
            }

            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));
            $graph->setDebug( true );
            $graph->load($uri);  
            $this->aParams['location'] = $graph->resource($uri)->getString('bus:location');
            $this->aParams['name'] = $graph->resource($uri)->getString('bus:name');
            $this->aParams['description'] = $graph->resource($uri)->getString('bus:description');
            $this->aParams['price'] = $graph->resource($uri)->getString('bus:price');
            $this->aParams['defaultImage'] = $graph->resource($uri)->getString('bus:defaultImage');

            $menufacturerUri = $graph->resource($uri)->getString('bus:hasManufacturer');
            $graph->load($menufacturerUri);  
            $this->aParams['menufacturerName'] = $graph->resource($menufacturerUri)->getString('bus:name');
            $this->aParams['menufacturerUri'] = $menufacturerUri;

            $categoryUri = $graph->resource($uri)->getString('bus:hasCategory');
            $graph->load($categoryUri);  
            $this->aParams['categoryName'] = $graph->resource($categoryUri)->getString('bus:name');
            $this->aParams['categoryUri'] = $categoryUri;

            $aListParentCategories = $graph->resource($uri)->getList('bus:childCategoryOf');
            $this->aParams['list_categories'] = implode(';',$aListParentCategories);
        }
    }
?>
